﻿// ==========================================================
// File: Universal.WebAPI.Core.HttpMethodOverrideMessageProcessor.cs
// Created: 14.12.2014
// Created By: Tim cadenbach
// 
// Copyright (C) 2014 Tim Cadenbach
// License: Apache License 2.0
// ==========================================================
using System.Net.Http;
using System.Threading;

namespace Universal.WebAPI.Handlers
{

  public class HttpMethodOverrideMessageProcessor : MessageProcessingHandler
  {

    protected override HttpRequestMessage ProcessRequest(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
    {

      if (request.Method == HttpMethod.Put)
      {
        request.Method = HttpMethod.Post;
        request.Headers.Add("X-HTTP-Method-Override", "PUT");
      }
      if (request.Method == HttpMethod.Delete)
      {
        request.Method = HttpMethod.Post;
        request.Headers.Add("X-HTTP-Method-Override", "DELETE");
      }
      return request;
    }

    protected override HttpResponseMessage ProcessResponse(
            HttpResponseMessage response,
            CancellationToken cancellationToken)
    {
      return response;
    }
  }
}
